<?php
/**
 * DMFramework
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @url http://dmframework.nl
 */

/**
 * The API can be used to fetch commands to a Directadmin webserver.
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.Library
 */
class Dmf_Directadmin
{
    /**
     * The directadmin server
     * 
     * @var string
     */
    private $_server;
    
    /**
     * Remote port (2222 most of the times)
     * 
     * @var int
     */
    private $_port;
    
    /**
     * Directadmin username
     * 
     * @var string
     */
    private $_username;
    
    /**
     * Directadmin password
     * 
     * @var string
     */
    private $_password;
    
    /**
     * GET or POST connection
     * 
     * @var Dmf_Http_Get | Dmf_Http_Post
     */
    private $_con;
    
    /**
     * Constructor
     * 
     * @param string $server
     * @param int $port
     */
    public function __construct( $server, $port = 80 )
    {
        $this->_server = $server;
        $this->_port = $port;
    }
    
    /**
     * Login credentials
     * 
     * @param string $user
     * @param string $pass
     */
    public function login( $user, $pass )
    {
        $this->_username = $user;
        $this->_password = $pass;
    }
    
    /**
     * Create a new request to the server
     * 
     * @param string $query
     * @param array $fields
     * @return string
     */
    public function query( $query, $fields = array() )
    {
        $http = new Dmf_Http();
        $options = array(
                        CURLOPT_PORT => $this->_port,
                        CURLINFO_HEADER_OUT => true,
                        CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
                        CURLOPT_USERPWD => $this->_username . ':' . $this->_password,
                        CURLOPT_SSL_VERIFYPEER => false, //Accept all SSL certificates if there is one
                        CURLOPT_HTTPHEADER => array(
                            'Connection'        => 'Close',
                            'Accept'            => '*/*',
                            'Host'              => $this->_server . ':' . $this->_port
                        )
                    );
        
        if ( count( $fields ) > 0 )
        {
            $this->_con = $http->post( $this->_server . '/' . $query, $fields, $options );
        }
        else
        {
            $this->_con = $http->get( $this->_server . '/' . $query, $options );
        }
        
        return $this->_con->getBody();
    }
    
    /**
     * Get request object
     * 
     * @return Dmf_Http_Get | Dmf_Http_Post
     */
    public function getCon()
    {
        return $this->_con;
    }
    
    /**
     * Create a query and fetch it's content
     * 
     * @param string $query
     * @param array $fields
     * @return array 
     */
    public function fetch( $query, $fields = array() )
    {
        $array = array();
        $content = $this->query( $query, $fields );
        
        if ( $content === false )
        {
            return false;
        }
        
        foreach( explode( '&', urldecode( $content ) ) as $value )
        {
            $info = explode( '=', $value );
            
            $array[ $info['0'] ] = $info['1'];
        }
        
        return $array;
    }
    
    /**
     * Get only the result
     * 
     * @param string $query
     * @param array $fields
     * @return boolean 
     */
    public function result( $query, $fields = array() )
    {
        $array = $this->fetch( $query, $fields );
        
        return isset( $array['error'] ) ? false : true;
    }
    
    /**
     * Check if the connection worked
     * 
     * @return boolean 
     */
    public function ok()
    {
        return $this->_con->ok();
    }
}